// import { setToken } from '@/utils/auth'

import { loginApi, getUserInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'

const state = () => ({
  token: getToken(),
  userInfo: {}

})
const getters = {

}

const mutations = {
  setToken: (state, token) => {
    // 将token存入 state
    state.token = token
    // 将token存入cookie
    setToken(token)
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
  },
  removeToken(start) {
    start.token = null
    removeToken()
  }
}

const actions = {
  // context上下文，传入参数
  async login(context, data) {
    // todo: 调用登录接口
    const { token } = await loginApi(data)
    // 返回一个token 123456
    // console.log(data)
    // console.log(res)
    console.log(token)
    // const token = res
    // debugger
    context.commit('setToken', token)
  },
  async getUserInfo(context) {
    // eslint-disable-next-line no-undef
    const res = await getUserInfo()
    console.log(res)
    context.commit('setUserInfo', res.data)
  },
  // 退出登录的action
  logout(context) {
    context.commit('removeToken') // 删除token
    context.commit('setUserInfo', {}) // 设置用户信息为空对象
  }
}
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
